<template>
	<div v-show="false">

	</div>
</template>

<script>
	import constants from '@/utils/constants.js';
	import {
		processResponse
	} from '@/utils/pageUtils.js';
	import utils from '../utils/utils.js';
	export default {
		name: "WebSocketComponent",
		props: ["componentModel"],
		data() {
			return {
				websocket: null
			}
		},
		inject: ['children'],
		mounted() {
			this.children.push(this);
			this.init();
		},
		beforeUnmount() {
			utils.removeArrayElement(this.children,this);
			this.websocket.close();
		},
		methods: {
			init() {
				this.websocket = new WebSocket(constants.ROOT_WEBSOCKET+this.componentModel.connectionString+"?X-Token="+window.localStorage.getItem("token"));
                var pageComponent=utils.getPageComponent(this);
				var that=this;
				this.websocket.onerror = function() {
					
				};

				this.websocket.onopen = function() {
					//alert("open");
				};

				this.websocket.onmessage = function(event) {
					processResponse(pageComponent, pageComponent,JSON.parse(event.data), that.children);
				};

				this.websocket.onclose = function() {
					//alert("close");
				};
			},
			setProp() {

			}
		},
	}
</script>

<style>
</style>
